function formatTime(date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function getDistance(lat1, lng1, lat2, lng2) {
  lat1 = lat1 || 0;
  lng1 = lng1 || 0;
  lat2 = lat2 || 0;
  lng2 = lng2 || 0;
  var rad1 = lat1 * Math.PI / 180.0;
  var rad2 = lat2 * Math.PI / 180.0;
  var a = rad1 - rad2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
  var r = 6378137;
  return ((r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)))) / 1000).toFixed(1)
}
function formatNumber(n) {
    n = n.toString()
    return n[1] ? n : '0' + n
}



function getWeek(len) {
    var weekday = new Array(7)
    weekday[0] = "周日";
    weekday[1] = "周一";
    weekday[2] = "周二";
    weekday[3] = "周三";
    weekday[4] = "周四";
    weekday[5] = "周五";
    weekday[6] = "周六";
    var result = [];
    var now = new Date();
    Date.prototype.getMonthDay = function () {
        return weekday[this.getDay()] + formatNumber(this.getMonth() + 1) + '月' + formatNumber(this.getDate()) + '日';
    }
    var str = '今天' + formatNumber(now.getMonth() + 1) + '月' + now.getDate() + '日';
    result.push(str);
    for (var i = 0; i < len - 1; i++) {
        now.setDate(now.getDate() + 1);
        result.push(now.getMonthDay())
    };
    return result;

}
  

// 格式化时间戳
function getTime( timestamp ) {
    var time = arguments[ 0 ] || 0;
    var t, y, m, d, h, i, s;
    t = time ? new Date( time ) : new Date();
    y = t.getFullYear();    // 年
    m = t.getMonth() + 1;   // 月
    d = t.getDate();        // 日
    h = t.getHours();       // 时
    i = t.getMinutes();     // 分
    s = t.getSeconds();     // 秒

    // 定义时间格式
    return y 
            + '-' 
            + ( m < 10 ? '0' + m : m ) 
            + '-' 
            + ( d < 10 ? '0' + d : d ) 
            + ' ' 
            + ( h < 10 ? '0' + h : h ) 
            + ':' 
            + ( i < 10 ? '0' + i : i ) 
            + ':' 
            + ( s < 10 ? '0' + s : s );
}
// 格式化时间戳
function getDate(timestamp) {
  var time = arguments[0] || 0;
  var t, y, m, d, h, i, s;
  t = time ? new Date(time) : new Date();
  y = t.getFullYear();    // 年
  m = t.getMonth() + 1;   // 月
  d = t.getDate();        // 日
  h = t.getHours();       // 时
  i = t.getMinutes();     // 分
  s = t.getSeconds();     // 秒

  // 定义时间格式
  return y
    + '-'
    + (m < 10 ? '0' + m : m)
    + '-'
    + (d < 10 ? '0' + d : d)
}
  
  
module.exports = {
    formatTime: formatTime,
    getWeek:getWeek,
    getTime:getTime,
    getDate: getDate,
    getDistance: getDistance
}
  